ggplot2(9)-整体外观

基础绘图

添加标题

1
2
3
4
5
6
7
library(ggplot2)
library(gcookbook)
## Warning: package 'gcookbook' was built under R version 3.4.4
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn)) + geom_point()
p + ggtitle("Age and Height of Schoolchildren")

mark

这个标题无法居中,要想居中,如下所示:

1
2
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn))+ geom_point()
p + labs(title="Age and Height of Schoolchildren")+theme(plot.title = element_text(hjust = 0.5))

mark
可以使用换行符()对标题进行换行:

1
p + ggtitle("Age and Height\nof Schoolchildren")

mark

对标题的位置进行修改

如果将标题移到绘图区域内部的时候,需要用vjust与ggtitle()进行配合:

1
2
p + ggtitle("Age and Height of Schoolchildren")+
theme(plot.title=element_text(vjust=-2.5))

mark

annotate对标题进行修改

另外对标题位置修改的方法就是使用文本型注解: annotate中的参数“text”表示注解的对象是文本。

1
p + annotate("text",x=mean(range(heightweight$ageYear)),y=Inf,label="Age and Height of Schoolchildren", vjust=1.5,size=6)

mark

修改文本外观

1
2
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn)) + geom_point()
p + theme(axis.title.x=element_text(size=16,lineheight=0.9,family="Times",face="bold.italic",colour="red"))

mark

修改标题

1
2
3
p + ggtitle("Age and Height\nof Schoolchildren")+
theme(plot.title=element_text(size=rel(1.5),lineheight=0.9,family="Times",face="bold.italic",colour="red"))
# lineheight=0.9可以调节标题的行间距

mark

图片上添加注解

1
p + annotate("text",x=15,y=53,label="Some text",size=7,family="Times",fontface="bold.italic",colour="red")

mark

为每个数据点添加注解

在下面的代码中,label=weightLb是将heightweight数据集中的weight映射到每个数据点上。

1
p + geom_text(aes(label=weightLb),size=4,family="Times",colour="red")

mark

使用主题

ggplot2自带的主题只有灰色与白色两种。

默认主题

默认主题是灰色的:

1
2
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()
p + theme_grey()

mark

更改为白色主题

1
p + theme_bw()

mark

可以对主题的基本字体与大小进行修改,如下所示:

1
p + theme_grey(base_size=16,base_family="Times")

mark

设置当前默认主题

1
2
theme_set(theme_bw())
p # 显示出来的是就是白色主题

mark

更改回原来的灰色主题:

1
2
theme_set(theme_grey()) # 改回灰色主题
p # 显示出来的是灰色主题

mark

修改主题元素的外观

绘图区域的修改:

1
2
3
4
5
6
7
8
9
10
11
12
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex)) + geom_point()
# 下面进行绘图区域的修改:
p + theme(
panel.grid.major = element_line(colour="red"),
#将主要的网络线的颜色改为红色(“red”)
panel.grid.minor = element_line(colour="red",linetype="dashed",size=0.2),
#将次级的网格改为红色(colour=“red”),线型是虚线(linetype=“dashed”),线宽是0.2(size=0.2)
panel.background = element_rect(fill="lightblue"),
#将绘图区域的背景色更改为浅蓝色(fill=“lightblue”)
panel.border = element_rect(colour="blue",fill=NA,size=2))
# 将绘图戴维的边界改为蓝色(colour=“blue”),中间不进行填充(fill=NA),线宽是2(size=20),如果fill=“red”,则绘图区域就要进行填充,遮挡数据点。

mark

文本项目的修改

1
2
3
4
5
6
7
8
9
10
p + ggtitle("Plot title here") +
theme(
axis.title.x = element_text(colour="green",size=14),
# 把x轴对应的标签文本更改为绿色(colour=“green”),尺寸为14(size=14)
axis.text.x = element_text(colour="blue"),
# 把x轴坐标刻度对应的数字更改为蓝色(colour=“blue”)
axis.title.y = element_text(colour="yellow",size=14,angle = 90),
# 把y轴对应的标签文本更改为黄色(colour=“yellow”),尺寸改为14(size=14),逆时针旋转90度(angle=90)
axis.text.y = element_text(colour="red",size=20,face="bold"))
# 把y轴坐标刻度对应的数字更改为红色(colour=“red”),尺寸改为20(size=20),加粗(face=“bold”)

mark

图例的修改

1
2
3
4
5
6
7
8
9
p + theme(
legend.background = element_rect(fill="grey45",colour="red",size=1),
# 把图例中的背景更改为45%的灰度(fill=“grey45”),边框颜色是红色(colour=“red”),尺寸为1(size=1)。
legend.title = element_text(colour="yellow",face="bold",size=14),
# 把图例标题文本更改为黄色(colour=“yellow”),加粗(face=“bold”),大小为14(size=14)。
legend.text = element_text(colour="red"),
# 把图例中的文本(注意区别与图例标题中的文本,图例标题的文本是“sex”,图例中的文本是“f”,“m”),更改为红色(colour=“red”)。
legend.key = element_rect(colour="blue",size=0.25))
# 把图例中的示例的边框改为蓝色(colour=“blue”),大小改为0.25(size=0.25)。

mark

对分面的修改

1
2
3
4
5
6
7
8
9
p +
facet_grid(sex~.) +
# 表示对性别进行分面。
theme(
strip.background = element_rect(fill="pink"),
# 表示对分面的区域进行填充,用粉色(fill=“pink”)。
strip.text.y = element_text(size=15,angle=-90,face="bold"))
# 表示对分面元素(这里是f与m)的标题进行更改,大小为15(size=15),顺时针旋转90度(angle=-90),加粗(face=“blod”)。

mark

主题更改的一些注意事项:

如果使用一套现成的主题并使用theme()微调其中的一些部分,则theme()必须接在指定主题的语句之后。否则theme()的设定不起作用,如下所示:

下面这段代码中,x轴的标签并不起作用,仍旧是黑色。

1
p + theme(axis.title.x= element_text(colour="red"))+theme_bw()

mark

下面的这段代码中x轴的标签起作用,是红色。

1
p + theme_bw() + theme(axis.title.x=element_text(colour="red",size=12))

mark

创建自定义主题

1
2
3
4
5
6
7
mytheme <- theme_bw() +
theme(text = element_text(colour="red"),
axis.title = element_text(size=rel(1.25)))
# 设置所有的文本是红色 axis.title = element_text(size=rel(1.25))
# 设置x轴与y轴的字体是当前主题的1.25倍。
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn)) + geom_point()
p + mytheme

mark

隐藏网格线

1
2
3
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn)) + geom_point()+theme_bw()
p + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())

mark

隐藏部分网格线

隐藏与x轴垂直的网格线

1
2
p + theme(panel.grid.major.x =element_blank(),
panel.grid.minor.x = element_blank())

mark

隐藏与y轴垂直的网格线:

1
2
p + theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

mark

与论文有关的绘图

写论文中的图表与书中有不太一样,需要网格线去掉,但加上x轴与y轴重合的直线,并且主题改为白色,

思路:

  1. 先设置白色主题:theme_bw()
  2. 移除网格线panel.grid.major =element_blank(),panel.grid.minor = element_blank()
  3. 移除四周的边框:panel.border=element_blank()
  4. 沿x轴与y轴显示直线:axis.line = element_line(colour=“black”)

操作如下:

1
2
3
4
5
p + theme_bw()+
theme(panel.grid.major =element_blank(),
panel.grid.minor = element_blank(),
panel.border=element_blank(),
axis.line = element_line(colour="black"))

mark

参考资料

  1. 常肖楠, 邓一硕, 魏太云. R数据可视化手册[M]. 人民邮电出版社, 2014.